package data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import dataTransferObject.LugarTemp;

public class JDBCLugarTempDAO implements ILugarTempDAO {

	private JDBCCategoriaDAO categoriaDAO;
	private JDBCZonaDAO zonaDAO;
	
	public JDBCLugarTempDAO() {
		categoriaDAO = new JDBCCategoriaDAO();
		zonaDAO = new JDBCZonaDAO();
	}
	
	public List<LugarTemp> selectAllLugares() {
		Connection conn = ConnectionManager.getInstance().CheckOut();
		PreparedStatement stmt = null;
		List<LugarTemp> listResult = new LinkedList<LugarTemp>();
		String sql = "SELECT * FROM lugar_temp";
		ResultSet result = null;
	
		try {
			stmt = conn.prepareStatement(sql);
			result = stmt.executeQuery();
		
			while (result.next()) {
				LugarTemp transfer = new LugarTemp();
				transfer.setIdLugarTemp(result.getInt("ID_LUGAR_TEMP"));
				transfer.setNombre(result.getString("NOMBRE"));
				transfer.setDescripcion(result.getString("DESCRIPCION"));
				transfer.setDireccion(result.getString("DIRECCION"));
				transfer.setHorario(result.getString("HORARIO"));
				transfer.setCategoria(categoriaDAO.selectCategoria(result.getInt("ID_CATEGORIA")));
				transfer.setZona(zonaDAO.selectZona(result.getInt("ID_ZONA")));
				listResult.add(transfer);
		}
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			ConnectionManager.getInstance().CheckIn(conn);
			try {
				if (result != null) {
					result.close();
				}
				if (stmt != null) {
					stmt.close();
				}
			} catch (Exception e) {
			}
		}
		return listResult;
	}

	public LugarTemp selectLugar(int idLugar) {
		Connection conn = ConnectionManager.getInstance().CheckOut();
		PreparedStatement stmt = null;
		LugarTemp transfer = new LugarTemp();
		String sql = "SELECT * FROM lugar_temp WHERE ID_LUGAR_TEMP = ?";
		ResultSet result = null;
		
		try {
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, idLugar);
			result = stmt.executeQuery();

			if (result.next()) {
				transfer.setIdLugarTemp(result.getInt("ID_LUGAR_TEMP"));
				transfer.setNombre(result.getString("NOMBRE"));
				transfer.setDescripcion(result.getString("DESCRIPCION"));
				transfer.setDireccion(result.getString("DIRECCION"));
				transfer.setHorario(result.getString("HORARIO"));
				transfer.setSuperficie(result.getInt("SUPERFICIE"));
				transfer.setServicios(result.getInt("SERVICIOS"));
				transfer.setBar(result.getInt("BAR"));
				transfer.setMascotas(result.getInt("MASCOTAS"));
				transfer.setAlimentacion(result.getInt("ALIMENTACION"));
				transfer.setAccesorios(result.getInt("ACCESORIOS"));
				transfer.setPeluqueria(result.getInt("PELUQUERIA"));
				transfer.setUrgencias(result.getInt("URGENCIAS"));
				transfer.setCategoria(categoriaDAO.selectCategoria(result.getInt("ID_CATEGORIA")));
				transfer.setZona(zonaDAO.selectZona(result.getInt("ID_ZONA")));
			}
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			ConnectionManager.getInstance().CheckIn(conn);
			try {
				if (result != null) {
					result.close();
				}
				if (stmt != null) {
					stmt.close();
				}
			} catch (Exception e) {
			}
		}	
		return transfer;
	}
	
	public void insertLugar(LugarTemp lugar) {
		Connection conn = ConnectionManager.getInstance().CheckOut();
		PreparedStatement stmt = null;
		String sql = "INSERT INTO lugar_temp" +
				" (NOMBRE, ID_CATEGORIA, ID_ZONA, DESCRIPCION, DIRECCION, HORARIO, SUPERFICIE, SERVICIOS, BAR," +
				" MASCOTAS, ALIMENTACION, ACCESORIOS, PELUQUERIA, URGENCIAS)" +
				" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		try {
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, lugar.getNombre());
			stmt.setInt(2, lugar.getCategoria().getIdCategoria());
			stmt.setInt(3, lugar.getZona().getIdZona());
			stmt.setString(4, lugar.getDescripcion());
			stmt.setString(5, lugar.getDireccion());
			stmt.setString(6, lugar.getHorario());
			stmt.setInt(7, lugar.getSuperficie());
			stmt.setInt(8, lugar.getServicios());
			stmt.setInt(9, lugar.getBar());
			stmt.setInt(10, lugar.getMascotas());
			stmt.setInt(11, lugar.getAlimentacion());
			stmt.setInt(12, lugar.getAccesorios());
			stmt.setInt(13, lugar.getPeluqueria());
			stmt.setInt(14, lugar.getUrgencias());
			
			stmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().CheckIn(conn);
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
	}
	
	public void deleteLugar(Connection conn, int idLugarTemp) {
		PreparedStatement stmt = null;
		String sql = "DELETE FROM lugar_temp WHERE ID_LUGAR_TEMP = ?";
		try {
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, idLugarTemp);
			stmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
	}
}
